Skip to content

[Woo POS] Coupons: Disallow adding duplicate coupons to cart #15551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 6, 2025

Conversation

iamgabrielma
Copy link
Contributor

@iamgabrielma iamgabrielma commented Apr 25, 2025

Description

Partially addresses WOOMOB-259 by disallowing duplicated coupons to be added to the cart, while we get feedback from design on how to handle it UI-wise.

We also avoid calling the pos_coupon_added_to_cart track event multiple times by creating a new implementation for POSItemActionHandler, otherwise we could see the track event called multiple times despite the action being disallowed, as we used the same action handler across products and coupons:

🔵 Tracked pos_coupon_added_to_cart, properties: [blog_id: -1, is_wpcom_store: false, plan: , was_ecommerce_trial: false, site_url: https://indiemelon.mystagingwebsite.com, store_id: c5bd46cc-1804-4f7b-badb-bb98c449127f]
🔵 Tracked pos_coupon_added_to_cart, properties: [is_wpcom_store: false, plan: , store_id: c5bd46cc-1804-4f7b-badb-bb98c449127f, was_ecommerce_trial: false, blog_id: -1, site_url: https://indiemelon.mystagingwebsite.com]
🔵 Tracked pos_coupon_added_to_cart, properties: [site_url: https://indiemelon.mystagingwebsite.com, store_id: c5bd46cc-1804-4f7b-badb-bb98c449127f, plan: , is_wpcom_store: false, blog_id: -1, was_ecommerce_trial: false]

Testing information

  • In POS, add a coupon to the cart
  • Try again, observe how nothing happens. Removing it allows to be re-added.
  • Confirm the pos_coupon_added_to_cart event is only logged once, when we add the coupon to the cart
  • Confirm that adding products and variations to cart still work as expected.

Screenshots

Screen.Recording.2025-04-25.at.10.34.12.mov

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on all devices (phone/tablet) and no regressions are added.

We cannot extract the function to make SUT for the whole test suite (at least not straight-away) as crashes due analytics running on different threads that we expect for each enum in the test suite.
Despite disallowing for duplicated coupons to be added to cart, the action handler is generic to all items so we were still calling the tracking event. We create a handler specifically for coupons to avoid calling the event when it shouldnt
@iamgabrielma iamgabrielma added type: task An internally driven task. feature: coupons Related to basic fulfillment such as order tracking. feature: POS labels Apr 25, 2025
@iamgabrielma iamgabrielma added this to the 22.3 milestone Apr 25, 2025
@iamgabrielma iamgabrielma marked this pull request as ready for review April 25, 2025 04:05
@iamgabrielma iamgabrielma requested a review from staskus April 25, 2025 04:06
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Apr 25, 2025

App Icon📲 You can test the changes from this Pull Request in WooCommerce iOS Prototype by scanning the QR code below to install the corresponding build.

App NameWooCommerce iOS Prototype
Build Number29672
VersionPR #15551
Bundle IDcom.automattic.alpha.woocommerce
Commitd004acc
Installation URL62bvkadr7jv2o
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@staskus
Copy link
Contributor

staskus commented Apr 25, 2025

@iamgabrielma thanks for the work!

Let's wait before merging until we get input from design. I think it's better right now to show an error in the checkout rather than not allow tapping the item without any visual signal.

Also, let's clarify if the item should be tappable when it's disabled (including the expired one). Maybe we wouldn't need a separate item handler logic.

Thanks!

@iamgabrielma
Copy link
Contributor Author

Confirmed with design FnDpCHVQ3Zpefad04ptbd4-fi-node-id=419-79264#1230020091 that nothing should happen when attempting to add a duplicated coupon to the cart. Will resolve the merge conflicts EOD and re-submit 👍

@wpmobilebot wpmobilebot modified the milestones: 22.3, 22.4 May 2, 2025
@wpmobilebot
Copy link
Collaborator

Version 22.3 has now entered code-freeze, so the milestone of this PR has been updated to 22.4.

…-twice

# Conflicts:
#	WooCommerce/Classes/POS/Presentation/ItemListView.swift
#	WooCommerce/WooCommerceTests/POS/Models/PointOfSaleAggregateModelTests.swift
Not that reusable, as analytics is not thread safe and crashes randomly some tests. For now let’s declare the dependencies per test basis
@staskus
Copy link
Contributor

staskus commented May 5, 2025

@iamgabrielma to confirm, is it ready for review?

Copy link
Contributor

@staskus staskus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just remove the unused code 👍

@@ -51,6 +51,28 @@ final class StandardPOSItemActionHandler: POSItemActionHandler {
}
}

@available(iOS 17.0, *)
final class CouponsItemActionHandler: POSItemActionHandler {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed, can be removed 👍

Copy link
Contributor Author

@iamgabrielma iamgabrielma May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I didn't see that we stopped using CouponsItemActionHandler since I worked on this bit. Removing it introduces a regression thought, as we treat products and coupons equally when dealing with track events, meaning: We will track the add_to_cart event despite coupons not being added to cart when are duplicated:

🔵 Tracked pos_coupon_added_to_cart, properties: [site_url: https://indiemelon.mystagingwebsite.com, blog_id: -1, was_ecommerce_trial: false, plan: , store_id: c5bd46cc-1804-4f7b-badb-bb98c449127f, is_wpcom_store: false]
🔵 Tracked pos_coupon_added_to_cart, properties: [store_id: c5bd46cc-1804-4f7b-badb-bb98c449127f, plan: , is_wpcom_store: false, blog_id: -1, was_ecommerce_trial: false, site_url: https://indiemelon.mystagingwebsite.com]

I've added a small helper function to handle this case in POSItemActionHandler, as this will happen both when we add coupons via list and via search.

I'll leave this one without merging for now in case you want to take a look, in a nutshell when adding coupons from either list or search, the track event should be logged just once.

@iamgabrielma
Copy link
Contributor Author

to confirm, is it ready for review?

Yes! Sorry I wasn't clear 🙇

Since the specific CouponsItemActionHandler is not used anymore, we still need to deal with duplicates coupons in the general handler to avoid duplicated track events
@@ -49,7 +49,9 @@ extension Cart {
case .variableParentProduct:
return
case .coupon(let coupon):
let couponItem = Cart.CouponItem(id: UUID(), code: coupon.code, summary: coupon.summary)
guard !coupons.contains(where: { $0.id == coupon.id }) else { return }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this check here if we skip the duplicate in the action handler?

Copy link
Contributor Author

@iamgabrielma iamgabrielma May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't anymore, I've removed this on 68096bc and added initial tests for the action handler on a7c5977

Copy link
Contributor

@staskus staskus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@iamgabrielma iamgabrielma enabled auto-merge May 6, 2025 07:21
@iamgabrielma iamgabrielma merged commit 77ca554 into trunk May 6, 2025
13 checks passed
@iamgabrielma iamgabrielma deleted the task/woomob-259-disallow-adding-same-coupon-twice branch May 6, 2025 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: coupons Related to basic fulfillment such as order tracking. feature: POS type: task An internally driven task.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants